<!doctype html>
<meta charset="utf-8">
<script>
    function createIframe () {
        var ifrm = document.createElement("iframe");
        ifrm.setAttribute("src", "child1.html");
        ifrm.setAttribute("id", "target");
        document.body.appendChild(ifrm);
    }
    function receiveMessage (event) {
        if (event.data == "execute") {
            var element = document.getElementById("target");
            element.contentWindow.location.href = "child2.html";
        }
    }
    window.addEventListener("message", receiveMessage, false);
    window.onload = function () {
        document.getElementById("prepare").onclick = createIframe;
        window.callPhantom(0);
    }
</script>
<button id="prepare">Prepare the iframe</button>
